package com.runchinaup.textapp.view;

/**
 * Created by liber on 2018/3/7.
 */

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * 折线图
 */
public class BrokenLine extends View {

    private int ScreenX;    //view的宽度

    private int ScreenY;    //view的高度

    private int paddingTop = 30;    //默认的上padding

    private int paddingLeft = 70;    //默认的左padding

    private int paddingRight = 30;    //默认的右padding

    private int paddingBottom = 50;    //默认的下padding

    private int numberOfX = 6;    //默认的X轴放6个

    private int numberOfY = 8;    //默认的Y轴放5个

    private boolean isDrawY = true;    //是否绘制Y轴

    private boolean isDrawX = true;     //是否绘制X轴

    private boolean isDrawInsideX = true;   //是否绘制内部的X轴

    private boolean isDrawInsideY = true;   //是否绘制内部的Y轴

    private boolean isAppendX = false;  // X轴是否向左突出一点

    private int appendXLength = 10;     // 向左X轴突出的长度

    private List<List<Float>> pointList;    // 传入的数据

    private List<String> titleYList;    // 计算得出的Y轴标题

    private float maxNumber = 0;    // Y轴最大值

    private List<String> titleXList;    // 传入的X轴标题

    private String xUnit = "";      // Y轴单位

    private String yUnit = "";      // Y轴单位


    public BrokenLine(Context context) {
        super(context);
    }

    public BrokenLine(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BrokenLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public BrokenLine(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    //测量画布大小
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureHeight = measureHeight(heightMeasureSpec);
        int measureWidth = measureWidth(widthMeasureSpec);
        setMeasuredDimension(measureWidth, measureHeight);

        ScreenX = measureWidth;
        ScreenY = measureHeight;

    }

    private int measureWidth(int measureSpec) {

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        int result = 400;
        if (specMode == MeasureSpec.AT_MOST) {
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }

        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = 300;
        if (specMode == MeasureSpec.AT_MOST) {
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }

    //计算X轴平均后的坐标
    private List<Point> initNumberOfX() {
        int num = (ScreenX - paddingLeft - paddingRight) / (numberOfX - 1);
        List<Point> list = new ArrayList<>();
        for (int i = 0; i < numberOfX; i++) {
            Point point = new Point();
            point.y = ScreenY - paddingTop - paddingBottom;
            point.x = paddingLeft + num * i;
            list.add(point);
        }
        return list;
    }

    //计算Y轴平均后的坐标
    private List<Point> initNumberOfY() {
        int num = (ScreenY - paddingBottom - paddingTop) / (numberOfY - 1);
        List<Point> list = new ArrayList<>();
        for (int i = 0; i < numberOfY; i++) {
            Point point = new Point();
            point.x = ScreenX - paddingLeft - paddingRight;
            point.y = ScreenY - paddingTop - paddingBottom - num * i;
            list.add(point);
        }
        return list;
    }


    //为Y轴找到最大值以及它的每个标题
    private void setYTitle(List<Point> listY, Canvas canvas) {

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(20f);
        paint.setTextAlign(Paint.Align.CENTER);

        if (pointList == null) {
            titleYList = new ArrayList<>();
            for (int i = 1; i <= numberOfY; i++) {
                int yAdd = 200 / numberOfY;
                titleYList.add(String.valueOf(200 - i * yAdd));
            }
        } else {
            for (int i = 0; i < pointList.size(); i++) {
                for (int j = 0; j < pointList.get(i).size(); j++) {
                    if (pointList.get(i).get(j) > maxNumber) {
                        maxNumber = pointList.get(i).get(j);
                    }
                }
            }
            maxNumber = maxNumber + maxNumber / 3;      //最大值
            titleYList = new ArrayList<>();
            for (int i = 0; i < numberOfY; i++) {
                titleYList.add(String.valueOf((int) (0 + i * (maxNumber / (numberOfY - 1)))));
            }
        }

        for (int i = 0; i < numberOfY; i++) {
            if (i != 0) {
                canvas.drawText(titleYList.get(i), paddingLeft - paddingLeft / 2, ScreenY - paddingBottom - listY.get(i).y, paint);
            } else {
                canvas.drawText(titleYList.get(i) + yUnit, paddingLeft - paddingLeft / 2, ScreenY - paddingBottom - listY.get(i).y, paint);
            }
        }
    }

    //为X轴找到最大值以及它的每个标题
    private void setXTitle(List<Point> listX, Canvas canvas) {

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(20f);

        if (pointList == null) {
            titleXList = new ArrayList<>();
            for (int i = 1; i <= numberOfX; i++) {
                titleXList.add(String.valueOf(100 / i));
            }
        } else {
            for (int i = 0; i < pointList.size(); i++) {
                for (int j = 0; j < pointList.get(i).size(); j++) {
                    if (pointList.get(i).get(j) > maxNumber) {
                        maxNumber = pointList.get(i).get(j);
                    }
                }
            }
            maxNumber = maxNumber + maxNumber / 3;      //最大值
            titleXList = new ArrayList<>();
            for (int i = 0; i < numberOfX; i++) {
                titleXList.add(String.valueOf((int) (0 + i * (maxNumber / (numberOfX - 1)))));
            }
        }

        for (int i = 0; i < numberOfX; i++) {
            if (i != 0) {
//                canvas.drawText(titleXList.get(i), paddingLeft - paddingLeft / 3, ScreenY - paddingBottom - listX.get(i).y, paint);
                canvas.drawText(titleXList.get(i), ScreenX - listX.get(i).x + paddingLeft / 2, ScreenY - paddingBottom + paddingBottom / 2, paint);
            } else {
//                canvas.drawText(titleYList.get(i) + yUnit, paddingLeft - paddingLeft / 3, ScreenY - paddingBottom - listY.get(i).y, paint);
                canvas.drawText(titleXList.get(i) + xUnit, ScreenX - listX.get(i).x + paddingLeft / 2, ScreenY - paddingBottom + paddingBottom / 2, paint);
            }
        }
    }


    //设置画布，画图
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(0xffA8A6A3);
        paint.setStyle(Paint.Style.FILL);


        List<Point> listY = initNumberOfY();
        List<Point> listX = initNumberOfX();

        //绘制X轴
        if (isDrawX) {
            int appendX = 0;
            if (isAppendX) {
                appendX = appendXLength;
            }
            canvas.drawLine(paddingLeft - appendX, paddingTop + listY.get(0).y, listY.get(0).x + paddingLeft
                    , paddingTop + listY.get(0).y, paint);
        }
        //绘制Y轴
        if (isDrawY) {
            canvas.drawLine(paddingLeft, paddingTop, paddingLeft, paddingTop + listX.get(0).y, paint);
        }
        //绘制内部X轴  绘制横向的线
        if (isDrawInsideX) {
            for (Point point : listY) {     //变化的Y轴所以用ListY
                if (!isDrawY) {
                    isDrawY = !isDrawY;
                    continue;
                }

                canvas.drawLine(paddingLeft, paddingTop + point.y, paddingLeft + point.x, paddingTop + point.y, paint);
            }
        }
        //绘制内部Y轴
        if (isDrawInsideY) {
            for (Point point : listX) {
                if (!isDrawX) {
                    isDrawX = !isDrawX;
                    continue;
                }
                canvas.drawLine(point.x, paddingTop, point.x, paddingTop + point.y, paint);
            }
        }


        setYTitle(listY, canvas);
        setXTitle(listX, canvas);
    }
}
